﻿using System.Collections.Generic;
using SoftConsept.Collections;
using TestBat.IModels.Domain;
using TestBat.IModels.Models;
using TestBat.IViewModels.Factories;
using TestBat.IViewModels.Workspaces.Schedules;
using TestBat.ViewModels.Base;

namespace TestBat.ViewModels.Workspaces.Schedules
{
    class ScheduleTaskViewModelComparer : IComparer<IScheduleTaskViewModel>
    {
        public int Compare(IScheduleTaskViewModel x, IScheduleTaskViewModel y)
        {
            return x.ScheduleTask.Sort - y.ScheduleTask.Sort;
        }
    }

    public class ScheduleTaskListViewModel : SortedListViewModel<IScheduleTaskViewModel>, IScheduleTaskListViewModel
    {
        public ScheduleTaskListViewModel(ISchedule schedule, IScheduleModel scheduleModel, IScheduleTaskViewModelFactory scheduleTaskViewModelFactory)
            : base(false)
        {
            this.scheduleModel = scheduleModel;
            this.schedule = schedule;
            this.scheduleTaskViewModelFactory = scheduleTaskViewModelFactory;
        }

        protected override ICollection<IScheduleTaskViewModel> _RecreateItems()
        {
            SortedObservableCollection<IScheduleTaskViewModel> list = new SortedObservableCollection<IScheduleTaskViewModel>(new ScheduleTaskViewModelComparer());

            foreach (IScheduleTask item in scheduleModel.GetScheduleTasks(schedule))
            {
                var vm = scheduleTaskViewModelFactory.Create(item);
                list.Add(vm);
            }

            return list;
        }

        private readonly ISchedule schedule;
        private readonly IScheduleModel scheduleModel;
        private readonly IScheduleTaskViewModelFactory scheduleTaskViewModelFactory;
    }
}
